
var Memberauth = function() {

    var datalist_url = $('.datalist_url').val();
    var checkhandle_url = $('.checkhandle_url').val();


    var initTable = function() {
        var table = $('#data-table');

        if(table.length > 0){
            table.dataTable({
                "processing": true,
                serverSide: true,// 开启服务器模式
                "ajax": {
                    url: datalist_url,
                },
                "autoWidth": false,
                "columns": [
                    {"data": "mobile"},
                    {
                        "data": "authType",
                        "render": function(data, type, row, meta) {
                            switch (data) {
                                case 1:
                                    data = '个人认证';
                                    break;
                                case 2:
                                    data = '企业认证';
                                    break;
                                default: break;
                            }

                            return data;
                        }
                    },
                    {"data": "createDate"},
                    {
                        "data": "checkStatus",
                        "render": function(data, type, row, meta) {
                            switch (data) {
                                case 1:
                                    data = '审核通过';
                                    break;
                                case 2:
                                    data = '待审核';
                                    break;
                                case 3:
                                    data = '审核驳回';
                                    break;
                                default: break;
                            }

                            return data;
                        }
                    },
                    {
                        "data": "null",
                        "render": function(data, type, row, meta) {
                            var html = '';
                            if (row.checkStatus == 2) {
                                html += '<button type="button" class="btn btn-success doCheck-btn">审核</button>';
                            }
                            //html += '<button type="button" class="btn btn-danger delete-btn">删除</button>';

                            return html;
                        }
                    }
                ],
                "language": {
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
                "pageLength": 10,
                "scrollX":"",
                "destroy": false,
                "stateSave": true,
                //"stateSave": false,
                "stateSaveParams": function (settings, data) {
                    var search = data.search.search;
                    //console.log(data);
                    if(search){
                        var arr = $.parseJSON(search);
                        for(var key in arr){
                            $("#searchForm input[name='"+key+"']").val(arr[key]);
                            $("#searchForm select[name='"+key+"']").val(arr[key]);
                        }
                    }
                },
                "order": [
                    [2, "desc"]
                ],
                "columnDefs": [{
                    "orderable": false,
                    "targets": [0, 1, 4]
                }],
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnInitComplete": function() {
                    //var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                    var city = $('input[name=city]').val();
                    if(city){
                        //$("#city").find("option:contains('"+city+"')").attr("selected", true);
                        get_xian($("#city"));
                    }
                }
            });
        }

        // 筛选
        $('#doSearch').on('click', function(event){
            var param = $('#searchForm').serializeObject();

            tableSearch(table, param);
        });

        // 审核
        table.on('click', '.doCheck-btn', function(event) {
            // 操作行对象
            var dataArr = table.DataTable().rows($(this).parents("tr")).data();
            var id = dataArr[0].id;
            var memberId = dataArr[0].memberId;
            var authType = dataArr[0].authType;
            var name = dataArr[0].name;
            var identityNumber = dataArr[0].identityNumber;
            var area = dataArr[0].area;

            $('#check-modal').find('.id').val(id);
            $('#check-modal').find('.memberId').val(memberId);
            $('#check-modal').find('.authType').val(authType);
            $('#check-modal').find('#name').text(name);
            $('#check-modal').find('#identityNumber').text(identityNumber);
            $('#check-modal').find('#area').text(area);
            $('#check-modal').modal('show');
        });
    };

    var tableSearch = function(table, params) {

        table.DataTable().search(JSON.stringify(params)).draw();
    };


    return {
        init: function() {
            initTable();

            this.onEvent();
        },

        onEvent : function(){

            // 审核状态
            $('.checkStatus').on('click', function() {
                var value = $(this).val();
                if (value == 1) {
                    $('.failMsg-div').hide();
                } else {
                    $('.failMsg-div').show();
                }
            });

            // 提交审核
            $('.save-btn').on('click', function() {
                // 操作行对象
                var id = $('input[name=id]').val();
                var memberId = $('input[name=memberId]').val();
                var authType = $('input[name=authType]').val();
                var checkStatus = $('.checkStatus:checked').val();
                var failMsg = '';
                if (checkStatus != 1) {
                    failMsg = $('input[name=failMsg]').val();
                    if (failMsg.trim() == '') {
                        layer.msg('请输入审核驳回的原因');
                        $('input[name=failMsg]').focus();
                        return;
                    }
                }

                var data = {
                    'id': id,
                    'memberId': memberId,
                    'authType': authType,
                    'value': checkStatus,
                    'failMsg': failMsg,
                };
                var url = checkhandle_url;

                $.ajax({
                    url: url,
                    type: 'POST',
                    data: data,
                    dataType : 'json',
                    success: function (data) {
                        layer.msg(data.msg);
                        if(data.code == 1){
                            var table = $('#data-table');
                            table.DataTable().ajax.reload();

                            $('#check-modal').modal('hide');
                        }
                    }
                });
            });
        }
    };

}();


//'use strict';

$(function() {
    Memberauth.init();

});